package com.example.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.example.dao.menuMapper.MenuMapper;
import com.example.dto.menu.Menu;
import com.example.dto.menu.MenuCriteria;
import com.example.exception.BadRequestException;
import com.example.server.MenuServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(tags = "系统：菜单管理")
@RequestMapping("/api/menu")
public class MenuController {

    @Autowired
    private MenuServer menuServer;
    @Autowired
    private MenuMapper menuMapper;
    private static final String ENTITY_NAME = "menu";

    @GetMapping(value = "/build")
    @ApiOperation("获取前端所需菜单")
    public ResponseEntity<Object> buildMenus() {
        Long userId = 1L;
        List<Menu> menuList = menuServer.findByUser(userId);
        List<Menu> menus = menuServer.buildTree(menuList);
        return new ResponseEntity<>(menuServer.buildMenus(menus), HttpStatus.OK);
    }

    @ApiOperation("返回全部的菜单")
    @GetMapping(value = "/lazy")
    public ResponseEntity<Object> queryAllMenu(@RequestParam Long pid) {
        return new ResponseEntity<>(menuServer.getMenus(pid), HttpStatus.OK);
    }

    @ApiOperation("根据菜单ID返回所有子节点ID，包含自身ID")
    @GetMapping(value = "/child")
    public ResponseEntity<Object> childMenu(@RequestParam Long id) {
        Set<Menu> menuSet = new HashSet<>();
        List<Menu> menuList = menuServer.getMenus(id);
        menuSet.add(menuServer.findOne(id));
        menuSet = menuServer.getChildMenus(menuList, menuSet);
        Set<Long> ids = menuSet.stream().map(Menu::getId).collect(Collectors.toSet());
        return new ResponseEntity<>(ids, HttpStatus.OK);
    }

    @ApiOperation("查询菜单")
    @GetMapping
    public ResponseEntity<Object> list(MenuCriteria criteria) {
        Map<String, Object> result = new HashMap<>(4);
        List<Menu> list =  menuServer.list(criteria);
        result.put("content",list);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @ApiOperation("查询菜单:根据ID获取同级与上级数据")
    @PostMapping("/superior")
    public ResponseEntity<Object> getMenuSuperior(@RequestBody List<Long> ids) {
        Set<Menu> menuDtos = new LinkedHashSet<>();
        if (CollectionUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                Menu menuDto = menuServer.findById(id);
                menuDtos.addAll(menuServer.getSuperior(menuDto, new ArrayList<>()));
            }
            return new ResponseEntity<>(menuServer.buildTree(new ArrayList<>(menuDtos)), HttpStatus.OK);
        }
        return new ResponseEntity<>(menuServer.getMenus(null), HttpStatus.OK);
    }

    @ApiOperation("增加菜单")
    @PostMapping
    public ResponseEntity<Object> createMenu(@RequestBody Menu menu) {
        if (menu.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        menu.setIFrame(Boolean.valueOf("false"));
        menuServer.create(menu);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @ApiOperation("删除菜单")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody List<Long> ids) {
        menuServer.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("修改菜单")
    @PutMapping
    public ResponseEntity<Object> updateMenu(@RequestBody Menu resources) {
        menuServer.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
